﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Lib;
using FrameWork.Lib.DataObjects;

namespace REVISIONMAX30.DataObjects
{
    public class Data : DataObjectBase
    {

        public Data()
        {
        }

        public Data(bool pNew)
            : base(true)
        {

            this.GroupList = new List<Group>();
            this.ConnectionStringList = new List<ConnectionString>();
        }

        public List<Group> GroupList { get; set; }

        public List<ConnectionString> ConnectionStringList { get; set; }

        public string MySQLBinPath { get; set; }

        public Result ValidateGroupName(string pGroupName)
        {
            var tResult = new Result();

            if (pGroupName.IsEmpty())
            {
                tResult.AddResult("Group Name must be specified.", ResultSeverityType.Error);
            }
            else if (this.GroupList.Where(t => t.GroupName.Equals(pGroupName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() != null)
            {
                tResult.AddResult("Group Name must be unique.", ResultSeverityType.Error);
            }

            return tResult;
        }

        public static Data Load(EventManager pEventManager)
        {
            pEventManager.AddResult(new Result("Loading Data....", ResultSeverityType.Info));

            Data data = null;

            if (System.IO.File.Exists(Common.Constants.dataFilePath))
            {
                try
                {
                    data = System.IO.File.ReadAllText(Common.Constants.dataFilePath).FromXML<Data>();
                }
                catch (Exception exc)
                {
                    pEventManager.AddResult(new Result(exc));

                    var tBackUpFileName = string.Format(Common.Constants.dataBackUpFilePath, Guid.NewGuid());

                    System.IO.File.Move(Common.Constants.dataFilePath, tBackUpFileName);

                    pEventManager.AddResult(new Result(string.Format("Data Main file was backed up to {0}", tBackUpFileName), ResultSeverityType.Info));
                }
            }

            if (data == null)
            {
                pEventManager.AddResult(new Result("Initializing new data main for storing data.", ResultSeverityType.Info));

                data = new Data(true);
            }

            if (data.MySQLBinPath.IsEmpty() && System.IO.Directory.Exists(Common.DefaultMySQLBatchPath))
            {
                data.MySQLBinPath = Common.DefaultMySQLBatchPath;
            }

            if (data.MySQLBinPath.HasValue() && !System.IO.Directory.Exists(data.MySQLBinPath))
            {
                pEventManager.AddResult(Result.NewError("Please set correct MySQL Bin path in settings."));
            }

            pEventManager.AddResult(new Result("Loading Data Completed!", ResultSeverityType.Info));

            return data;
        }

        public void Save(EventManager pEventManager)
        {
            var tXmlData = this.ToXml();

            try
            {
                pEventManager.AddResult(new Result("Saving Data....", ResultSeverityType.Info));
                System.IO.File.WriteAllText(Common.Constants.dataFilePath, tXmlData);
                pEventManager.AddResult(new Result("Saved Data!", ResultSeverityType.Info));
            }
            catch (Exception exc)
            {
                pEventManager.AddResult(new Result(exc));

                var tBackUpFileName = string.Format(Common.Constants.dataBackUpFilePath, Guid.NewGuid());

                System.IO.File.WriteAllText(Common.Constants.dataSaveBackUpFilePath, tXmlData);

                pEventManager.AddResult(new Result(string.Format("Data Main Save file was backed up to {0}.", tBackUpFileName), ResultSeverityType.Info));
            }


        }
    }
}
